//
//  WSMusicViewController.m
//  RunningMan
//
//  Created by 王帅 on 16/5/23.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSMusicViewController.h"
#import "WSMyTool.h"
#import "WSMusicModel.h"
#import "WSMusicCollectionViewCell.h"
#import "WSAFNetWorkingTool.h"
#import "WSMusicDetailsViewController.h"
@interface WSMusicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *musicArray;

@end

@implementation WSMusicViewController

-(NSMutableArray *)musicArray {
    if (!_musicArray) {
        _musicArray = [NSMutableArray array];
    }
    return _musicArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"音乐";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self createCollectionView];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH,  HEIGHT - 113) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    layout.itemSize = CGSizeMake(190 * FITWIDTH, 170 * FITHEIGHT);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 20);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    [self.collectionView registerClass:[WSMusicCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([WSMusicCollectionViewCell class])];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getData];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self getMoreData];
    }];
    
    [self.collectionView.mj_header beginRefreshing];
}



- (void)getData {
    [WSAFNetWorkingTool getWithURL:@"http://wawa.fm:9090/wawa/api.php/user/gethotplaylist?datatype=jsonp&r=10&page=1&callback=data&_=1459133646101" parameter:nil httpHeader:@{@"Host" : @"wawa.fm:"} responsType:ResponseTypeDATA progress:^(NSProgress *progress) {} success:^(id result) {
        
        NSString *str = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
        
        NSString *str1 = [str substringFromIndex:7];
        NSString *str2 = [str1 substringToIndex:str1.length - 1];
        
        
        NSData *data = [[NSData alloc] initWithData:[str2 dataUsingEncoding:NSUTF8StringEncoding]];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];\
        self.musicArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            WSMusicModel *model = [[WSMusicModel alloc] initWithDic:dic];
            [self.musicArray addObject:model];
        }
        
        [self.collectionView reloadData];
        
        [self.collectionView.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
        
        
    }];

    
}

- (void)getMoreData {
    static NSInteger page = 1;
    page++;
    NSString *url = [NSString stringWithFormat:@"http://wawa.fm:9090/wawa/api.php/user/gethotplaylist?datatype=jsonp&r=10&page=%ld&callback=data&_=1459133646101", page];
    [WSAFNetWorkingTool getWithURL:url parameter:nil httpHeader:@{@"Host" : @"wawa.fm:"} responsType:ResponseTypeDATA progress:^(NSProgress *progress) {
        
        
    } success:^(id result) {
        
        NSString *str = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
        
        NSString *str1 = [str substringFromIndex:7];
        NSString *str2 = [str1 substringToIndex:str1.length - 1];
        
        
        NSData *data = [[NSData alloc] initWithData:[str2 dataUsingEncoding:NSUTF8StringEncoding]];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in array) {
            WSMusicModel *model = [[WSMusicModel alloc] initWithDic:dic];
            [self.musicArray addObject:model];
        }
        
        [self.collectionView reloadData];
        
        [self.collectionView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.musicArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WSMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WSMusicCollectionViewCell class]) forIndexPath:indexPath];
    WSMusicModel *model = self.musicArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WSMusicDetailsViewController *details = [[WSMusicDetailsViewController alloc] init];
    WSMusicModel *model = self.musicArray[indexPath.row];
    details.ID = model.ID;
    [self.navigationController pushViewController:details animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
